const { Builder, By, Key, until } = require('selenium-webdriver');
const { Options } = require('selenium-webdriver/chrome');


(async function myFunction() {

  require('chromedriver');
  let yapiExtension = './cross-request-master.zip';
  var webdriver = require('selenium-webdriver');

  let driver = await new webdriver.Builder().forBrowser('chrome')
    .setChromeOptions(new Options().addExtensions([yapiExtension]))
    .build();

  try {
    // 点击登陆
    await driver.get("http://localhost:40001")
    await driver.findElement(By.css('button[type=button]')).click()
    await new Promise(res => setTimeout(res, 1000));

    // 输入用户名密码
    await driver.findElement(By.id("email")).sendKeys("admin@lfxstek.top")
    await driver.findElement(By.id("password")).sendKeys("admin")
    await new Promise(res => setTimeout(res, 1000));

    // 点击确认
    await driver.findElement(By.css('button[type=submit]')).click()
    await new Promise(res => setTimeout(res, 1000));

    // 点击project
    await driver.findElement(By.xpath("//*[text()='project']/..")).click();
    await new Promise(res => setTimeout(res, 1000));

    // 选择yapi项目
    await driver.findElement(By.xpath("//*[text()='yapi']/../..")).click();
    await new Promise(res => setTimeout(res, 1000));

    // 点击测试合集
    await driver.findElement(By.xpath("//*[text()='测试集合']/.")).click();
    await new Promise(res => setTimeout(res, 1000));

    // 选择appsrv测试合集
    await driver.findElement(By.xpath("//*[text()='appsrv']/../..")).click();
    await new Promise(res => setTimeout(res, 1000));

    // 测试接口数量
    let test_len = await driver.findElements(By.xpath("//tbody/tr"));
    console.log("测试接口数量：" + test_len.length);

    // 点击开始测试
    await driver.findElement(By.xpath("//*[text()='开始测试']/..")).click();
    await new Promise(res => setTimeout(res, 1000));

    let success = await driver.findElements(By.className("anticon anticon-check-circle"))
    console.log("成功数量：" + success.length);

    let fail = await driver.findElements(By.className("anticon anticon-info-circle"))
    console.log("失败数量：" + fail.length);

  } catch (error) {
    console.error(error);
  } finally {
    // 关闭浏览器
    // await driver.quit();
  }
})();